Display mesh in 3D and using color on 2D¶

In [3]:
import pandas as pd
import holoviews as hv

Read in mesh from hgrid.gr3 file¶

In [4]:
from schimpy import schism_mesh

smesh = schism_mesh.read_mesh('../tests/data/m1_hello_schism/hgrid.gr3')
In [5]:
dfelems = pd.DataFrame(smesh.elems,columns=[0,1,2])
dfelems
Out[5]:
0 1 2
0 0 2 1
1 1 4 3
2 2 5 4
3 3 7 6
4 4 8 7
... ... ... ...
4631 2630 2631 2626
4632 2631 2632 2627
4633 2633 2634 2630
4634 2634 2635 2631
4635 2636 2637 2634

4636 rows × 3 columns

replace 'z' with negative values for depth

In [6]:
dfnodes = pd.DataFrame(smesh.nodes, columns=['x','y','z'])
dfnodes['depth'] = -dfnodes.z
dfnodes = dfnodes.drop(columns=['z'])
dfnodes
Out[6]:
x y depth
0 56000.00 -10400.00 -0.500000
1 55831.58 -10400.00 -0.500000
2 56000.00 -10350.00 -0.944444
3 55663.16 -10400.00 -0.500000
4 55831.58 -10348.07 -1.023290
... ... ... ...
2634 55831.58 10348.07 -1.023290
2635 55663.16 10400.00 -0.500000
2636 56000.00 10350.00 -0.944444
2637 55831.58 10400.00 -0.500000
2638 56000.00 10400.00 -0.500000

2639 rows × 3 columns

TriSurface drawn using nodes and elements¶

Adapted from https://anaconda.org/philippjfr/brain/notebook?version=2017.05.04.1924

The code below allows for the simplices already defined by elems to be used instead of doing a Delaunay triangulation (used from scipy as a way to calculate the simplices)

In [7]:
# uncomment and the run install script below if plotly is not available
#!conda install -y -c conda-forge plotly

hv.extension('plotly')

import param

class TriSurface(hv.TriSurface):
    
    simplices = param.Array()

class TriSurfacePlot(hv.plotting.plotly.TriSurfacePlot):

    style_opts = ['cmap', 'plot_edges']

    def get_data(self, element, ranges, style, **kwargs):
        if element.simplices is None:
            return super(TriSurfacePlot, self).get_data(element, ranges, style, **kwargs)
        x, y, z = (element.dimension_values(i) for i in range(3))
        simplices = element.simplices
        return [dict(x=x, y=y, z=z, simplices=simplices)]
    
hv.Store.register({TriSurface: TriSurfacePlot}, 'plotly')

tris = TriSurface(dfnodes, simplices = dfelems.values).opts(width=800, height=800, cmap='blues_r')
tris.opts(plot_edges=False, colorbar=True)
Out[7]:

Save mesh as html to embed in docs

In [8]:
#hv.save(tris,'mesh_surface_colored_by_depth.html')

Trimesh to show z values with color¶

Alternate way of displaying the z (depth values) with color

In [9]:
hv.extension('bokeh')
from holoviews.operation import datashader
trimesh = hv.TriMesh((dfelems.values, hv.Points(dfnodes,vdims='depth'))).opts(cmap='fire',node_alpha=0,edge_color='z',filled=True)
img = datashader.rasterize(trimesh).opts(cmap='rainbow4', colorbar=True, tools=['hover'], width=800)
img
Out[9]:
In [10]:
#hv.save(img,'mesh_colored_by_depth.html')
In [11]:
import panel as pn
pn.extension()
In [12]:
pn.Row(tris, img).show()
Launching server at http://localhost:51232
Out[12]:
<panel.io.server.Server at 0x2a2566f70>
ERROR:tornado.application:Uncaught exception GET / (::1)
HTTPServerRequest(protocol='http', host='localhost:51232', method='GET', uri='/', version='HTTP/1.1', remote_ip='::1')
Traceback (most recent call last):
  File "/Users/nickysandhu/miniforge3/envs/schism_viz/lib/python3.8/site-packages/holoviews/plotting/renderer.py", line 538, in plotting_class
    plotclass = Store.registry[cls.backend][element_type]
KeyError: <class '__main__.TriSurface'>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/nickysandhu/miniforge3/envs/schism_viz/lib/python3.8/site-packages/tornado/web.py", line 1713, in _execute
    result = await result
  File "/Users/nickysandhu/miniforge3/envs/schism_viz/lib/python3.8/site-packages/panel/io/server.py", line 338, in get
    session = await self.get_session()
  File "/Users/nickysandhu/miniforge3/envs/schism_viz/lib/python3.8/site-packages/bokeh/server/views/session_handler.py", line 144, in get_session
    session = await self.application_context.create_session_if_needed(session_id, self.request, token)
  File "/Users/nickysandhu/miniforge3/envs/schism_viz/lib/python3.8/site-packages/bokeh/server/contexts.py", line 243, in create_session_if_needed
    self._application.initialize_document(doc)
  File "/Users/nickysandhu/miniforge3/envs/schism_viz/lib/python3.8/site-packages/panel/io/server.py", line 297, in initialize_document
    super().initialize_document(doc)
  File "/Users/nickysandhu/miniforge3/envs/schism_viz/lib/python3.8/site-packages/bokeh/application/application.py", line 194, in initialize_document
    h.modify_document(doc)
  File "/Users/nickysandhu/miniforge3/envs/schism_viz/lib/python3.8/site-packages/bokeh/application/handlers/function.py", line 143, in modify_document
    self._func(doc)
  File "/Users/nickysandhu/miniforge3/envs/schism_viz/lib/python3.8/site-packages/panel/io/server.py", line 132, in _eval_panel
    doc = as_panel(panel)._modify_doc(server_id, title, doc, location)
  File "/Users/nickysandhu/miniforge3/envs/schism_viz/lib/python3.8/site-packages/panel/viewable.py", line 265, in _modify_doc
    return self.server_doc(doc, title, location) # type: ignore
  File "/Users/nickysandhu/miniforge3/envs/schism_viz/lib/python3.8/site-packages/panel/viewable.py", line 871, in server_doc
    model = self.get_root(doc)
  File "/Users/nickysandhu/miniforge3/envs/schism_viz/lib/python3.8/site-packages/panel/viewable.py", line 559, in get_root
    root = self._get_model(doc, comm=comm)
  File "/Users/nickysandhu/miniforge3/envs/schism_viz/lib/python3.8/site-packages/panel/layout/base.py", line 146, in _get_model
    objects = self._get_objects(model, [], doc, root, comm)
  File "/Users/nickysandhu/miniforge3/envs/schism_viz/lib/python3.8/site-packages/panel/layout/base.py", line 131, in _get_objects
    child = pane._get_model(doc, root, model, comm)
  File "/Users/nickysandhu/miniforge3/envs/schism_viz/lib/python3.8/site-packages/panel/pane/holoviews.py", line 265, in _get_model
    plot = self._render(doc, comm, root)
  File "/Users/nickysandhu/miniforge3/envs/schism_viz/lib/python3.8/site-packages/panel/pane/holoviews.py", line 342, in _render
    return renderer.get_plot(self.object, **kwargs)
  File "/Users/nickysandhu/miniforge3/envs/schism_viz/lib/python3.8/site-packages/holoviews/plotting/bokeh/renderer.py", line 70, in get_plot
    plot = super().get_plot(obj, doc, renderer, **kwargs)
  File "/Users/nickysandhu/miniforge3/envs/schism_viz/lib/python3.8/site-packages/holoviews/plotting/renderer.py", line 231, in get_plot
    plot = self_or_cls.plotting_class(obj)(obj, renderer=renderer,
  File "/Users/nickysandhu/miniforge3/envs/schism_viz/lib/python3.8/site-packages/holoviews/plotting/renderer.py", line 540, in plotting_class
    raise SkipRendering(f"No plotting class for {element_type.__name__} found")
holoviews.core.options.SkipRendering: No plotting class for TriSurface found
ERROR:tornado.access:500 GET / (::1) 9.49ms
WARNING:tornado.access:404 GET /favicon.ico (::1) 0.24ms
In [ ]: